﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Mammoth.Entities;
using Mammoth.Services;
using Mammoth.Views;
using Mammoth.Views.Account;

namespace Mammoth.ViewModels
{
    public class BuddyListViewModel : Libs.WPFViewModelBase, IEventInterest
    {
        public static readonly RoutedCommand ApproveCommand = new RoutedCommand();
        public static readonly RoutedCommand BlockCommand = new RoutedCommand();

        public ICollectionView BuddyList { get; private set; }
        public IGTalkService GTalkService { get; private set; }

        public BuddyListViewModel(IInputElement dispatcher, IGTalkService service)
            : base(dispatcher)
        {
            GTalkService = service;
            
            GTalkService.FriendListLoad += GTalkService_FriendListLoad;
            GenerateBuddyListAsync();
        }

        private void GenerateBuddyListAsync()
        {
            Task.Factory.StartNew(GenerateBuddyList);
        }

        private void GenerateBuddyList()
        {
            var source = new ObservableCollection<BuddyEntity>(GTalkService.BuddyList);
            App.Current.Dispatcher.Invoke(() =>
            {
                BuddyList = CollectionViewSource.GetDefaultView(source);
                BuddyList.SortDescriptions.Add(new SortDescription("PresenceType", ListSortDirection.Ascending));
            });
        }

        public void ReleaseHandler()
        {
            GTalkService.FriendListLoad += GTalkService_FriendListLoad;
        }

        void GTalkService_FriendListLoad(object sender, Events.ServiceEventArgs e)
        {
            GenerateBuddyListAsync();
        }

        public void ApproveHandler(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                var buddy = (BuddyEntity)e.Parameter;
                Approve(buddy.ServiceEmail, buddy.Email);
            }
            catch (Exception ex)
            {
                Dispatcher.RaiseErrorEvent(ex.Message);
            }
        }

        public void BlockHandler(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                var buddy = (BuddyEntity)e.Parameter;
                Block(buddy.ServiceEmail, buddy.Email);
            }
            catch (Exception ex)
            {
                Dispatcher.RaiseErrorEvent(ex.Message);
            }
        }

        public void Approve(string serviceEmail, string buddyEmail)
        {
            GTalkService.ApproveSubscirption(serviceEmail, buddyEmail);
        }

        public void Block(string serviceEmail, string buddyEmail)
        {
            GTalkService.BlockBuddy(serviceEmail, buddyEmail);
        }

        public void ShowAddBuddy()
        {
            Dispatcher.RaiseNavigationEvent(Events.NavigationType.Show, typeof(AddBuddyForm));
        }

    }
}
